package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/travelComments")
public class TravelCommentController {
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAiteService aiteService;
    /**
     * 添加评论的操作
     * @param travelComment
     * @return
     */
    @PostMapping
    public Object addComment(TravelComment travelComment){
        JSONResult jsonResult = new JSONResult();
        try {
        //当前的用户
        User currentUser = UserContext.getCurrentUser();
        if (currentUser==null){
            throw  new RuntimeException("用户没有登录");
        }
        //当有艾特时添加艾特记录
        List<User> users = userService.selectAll();
        Aite aite = new Aite();
        User target = new User();
        for (User user : users) {
            String nickName = user.getNickName();
            String aiteName = "@"+ nickName;
            //如果内容中存在用户
            if (travelComment.getContent().contains(aiteName)&&nickName!=currentUser.getNickName()){
                target.setId(user.getId());
                aite.setTarget(target);
                aite.setUser(currentUser);
                aite.setTravel(travelComment.getTravel());
                aiteService.insertRecord(aite);
            }
        }
        //页面传来的参数,游记的id,评论的内容
        travelComment.setReviewer(currentUser);
            //增加评论
            travelCommentService.insert(travelComment);
            //记录该游记的评论数量加一
            travelService.addCommentNum(travelComment);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    /**
     * 保存回复评论
     * @param commentReply
     * @return
     */
    @PostMapping("/{travelId}/reply")
    public Object addCommentReply(CommentReply commentReply,@PathVariable Long travelId){
        User currentUser = UserContext.getCurrentUser();

        //当有艾特时添加艾特记录
        List<User> users = userService.selectAll();
        Aite aite = new Aite();
        User target = new User();
        for (User user : users) {
            String nickName = user.getNickName();
            String aiteName = "@"+ nickName;
            //如果内容中存在用户
            if (commentReply.getContent().contains(aiteName)&&nickName!=currentUser.getNickName()){
                target.setId(user.getId());
                aite.setTarget(target);
                aite.setUser(currentUser);
                Travel travel = new Travel();
                travel.setId(travelId);
                aite.setTravel(travel);
                aiteService.insertRecord(aite);
            }
        }
        //回复者的对象
        commentReply.setUser(currentUser);
        JSONResult jsonResult = new JSONResult();
        try {
            //增加评论回复记录
            travelCommentService.insert(commentReply);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
}
